package com.cnswhy.cloud.server.merchant.controller.merchant;

import com.cnswhy.cloud.common.redis.service.IdGeneratorService;
import com.cnswhy.cloud.server.core.entity.MerchantSetting;
import com.cnswhy.cloud.server.core.dto.MerchantSettingSaveDTO;
import com.cnswhy.cloud.server.core.dto.MerchantSettingUpdateDTO;
import com.cnswhy.cloud.server.core.dto.MerchantSettingPageDTO;
import com.cnswhy.cloud.server.core.entity.Slideshow;
import com.cnswhy.cloud.server.core.service.MerchantSettingService;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.cnswhy.cloud.common.core.base.controller.SuperController;
import com.cnswhy.cloud.common.core.base.R;
import com.cnswhy.cloud.server.merchant.util.OssUtils;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
//import com.github.zuihou.security.annotation.PreAuth;
import org.springframework.web.bind.annotation.RestController;


/**
 * <p>
 * 前端控制器
 * 店铺设置基本信息
 * </p>
 *
 * @author cnswhy
 * @date 2020-10-23
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/merchantSetting")
@Api(value = "MerchantSetting", tags = "店铺设置基本信息")
//@PreAuth(replace = "merchantSetting:")
public class MerchantSettingController extends SuperController<MerchantSettingService, Long, MerchantSetting, MerchantSettingPageDTO, MerchantSettingSaveDTO, MerchantSettingUpdateDTO> {
    @Autowired
    IdGeneratorService idGeneratorService;
    @PostMapping("/syncUrl")
    public R<Boolean> syncUrl() {
        List<MerchantSetting> merchantSettings = baseService.list();
        for(MerchantSetting merchantSetting : merchantSettings){
            if(StringUtils.isBlank(merchantSetting.getBackgroundUrl()) && StringUtils.isNotBlank(merchantSetting.getBackgroundImg())){
                merchantSetting.setBackgroundUrl(OssUtils.updateLoadFileToOss("mstt", "http://139.9.69.116:81/" + merchantSetting.getBackgroundImg() ,idGeneratorService.genCommonIdString() + ".jpg"));
            }
            if(StringUtils.isBlank(merchantSetting.getLogoUrl()) && StringUtils.isNotBlank(merchantSetting.getLogoImg())){
                merchantSetting.setLogoUrl(OssUtils.updateLoadFileToOss("mstt",  "http://139.9.69.116:81/" +merchantSetting.getLogoUrl() ,idGeneratorService.genCommonIdString() + ".jpg"));
            }
            if(StringUtils.isBlank(merchantSetting.getPosterUrl()) && StringUtils.isNotBlank(merchantSetting.getPosterImg())){
                merchantSetting.setPosterUrl(OssUtils.updateLoadFileToOss("mstt", "http://139.9.69.116:81/" + merchantSetting.getPosterUrl() ,idGeneratorService.genCommonIdString() + ".jpg"));
            }

            baseService.updateById(merchantSetting);
        }
        return success();
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<MerchantSetting> merchantSettingList = list.stream().map((map) -> {
            MerchantSetting merchantSetting = MerchantSetting.builder().build();
            return merchantSetting;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(merchantSettingList));
    }
}
